from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select

from utils.java_script_exec import JavaScriptExec


class PageEcshopBackGoodsAdd():
    ''' 后台商品添加 '''
    def __init__(self, driver):

        self.driver = driver
        # 元素定位器
        self.loc_goods_add_goods_name = (By.NAME, "goods_name") # 商品名输入框
        self.loc_goods_add_cat_id = (By.NAME,'cat_id') # 分类下拉框
        self.loc_goods_add_shop_price = (By.XPATH, '//input[@name="shop_price"]')  # 输入金额
        self.loc_goods_add_submit = (By.XPATH, '//div[@id="tabbody-div"]/form/div/input[2]')  # 分类下拉选择
        

    def goods_add_goods_name(self,goods_name):
        ''' 输入用户名 '''
        # self.driver.find_element(*self.loc_goods_add_goods_name).clear()
        self.driver.find_element(*self.loc_goods_add_goods_name).send_keys(goods_name)

    def goods_add_cat_id(self):
        ''' 选择分类 '''
        option = self.driver.find_element(*self.loc_goods_add_cat_id)
        Select(option).select_by_value("132")

    def goods_add_shop_price(self,shop_price):
        self.driver.find_element(*self.loc_goods_add_shop_price).clear()
        self.driver.find_element(*self.loc_goods_add_shop_price).send_keys(shop_price)

    def goods_add_submit(self):
        self.driver.find_element(*self.loc_goods_add_submit).click()

    def goods_add(self ,goods_name ,shop_price ):
        self.goods_add_goods_name(goods_name)
        self.goods_add_cat_id()
        self.goods_add_shop_price(shop_price)
        jsexec = JavaScriptExec(self.driver)
        jsexec.js_scroll()
        self.goods_add_submit()

